/*
 * Project: Daycare Web Application
 * Package: com.madetoordersoftware.daycare.control
 * File:    EmailCtrl
 * Created: Jan 17, 2014
 */
/**
 * Copyright (c) 2010 Made 2 Order Software, LLC
 *
 * All rights reserved. No part of this software may be reproduced, stored in a retrieval system, or
 * transmitted in any form or by any means, without the prior written permission of Made 2 Order Software.
 *
 * You may not distribute the URL or redistribute the files to third parties. You may transfer the files onto
 * a single storage device so long as you can prove ownership of the software. You may not reverse engineer,
 * decompile, or disassemble the software or media files. You may not modify or translate the software or
 * media, or distribute copies of the software or media without the written consent of Made 2 Order Software,
 * LLC.
 */
package com.made2ordersoftware.daycare.control;

import com.made2ordersoftware.control.AbstractCtrl;
import java.util.Properties;
import javax.enterprise.context.RequestScoped;
import javax.inject.Named;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author David
 */
@Named("email")
@RequestScoped
public class EmailCtrl extends AbstractCtrl {

    private static final Logger LOG = LoggerFactory.getLogger(EmailCtrl.class);
    private static final long serialVersionUID = 1L;
    private static final String SMTP_AUTH_USER = "dcannin";
    private static final String SMTP_AUTH_PWD = "April1969";
    private static final String HOST = "smtp.gmail.com";
    private static final String TO = "tedlai82@gmail.com";
    private String name;
    private String company;
    private String from;
    private String subject;
    private String message;

    /**
     * Creates a new instance of <code>EmailCtrl</code>.
     */
    public EmailCtrl() {
    }

    /**
     *
     * @return
     */
    public String sendEmail() {
        String nextPage = "/confirmation.xhtml";

        Properties props = new Properties();
        props.put("mail.transport.protocol", "smtp");
        props.put("mail.host", HOST);
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.port", "465");
        props.put("mail.smtp.socketFactory.port", "465");
        props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        props.put("mail.smtp.socketFactory.fallback", "false");

        Session session = Session.getDefaultInstance(props, new javax.mail.Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(SMTP_AUTH_USER, SMTP_AUTH_PWD);
            }
        });

        try {
            MimeMessage email = new MimeMessage(session);
            email.setSender(new InternetAddress(from));
            email.setSubject(getSubject());
            email.setContent(formatEmailMessage(), "text/plain");
            email.setRecipients(Message.RecipientType.TO, InternetAddress.parse(TO));
            Transport.send(email);
        } catch (MessagingException e) {
            nextPage = "/home.xhtml";
            e.printStackTrace();
        }

        return nextPage;
    }

    private String formatEmailMessage() {
        StringBuilder stbr = new StringBuilder();
        stbr.append("Name:  ").append(getName());
        stbr.append("\nCompany:  ").append(getCompany());
        stbr.append("\n\n\n").append(getMessage());
        LOG.info("[DEBUG] //////////////////////////////////");
        LOG.info(stbr.toString());
        LOG.info("[DEBUG] //////////////////////////////////");
        return stbr.toString();
    }

    /**
     *
     * @return
     */
    public String getCompany() {
        return company;
    }

    /**
     *
     * @param company
     */
    public void setCompany(String company) {
        this.company = company;
    }

    /**
     *
     * @return
     */
    public String getFrom() {
        return from;
    }

    /**
     *
     * @param from
     */
    public void setFrom(String from) {
        this.from = from;
    }

    /**
     *
     * @return
     */
    public String getName() {
        return name;
    }

    /**
     *
     * @param name
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * @return the subject
     */
    public String getSubject() {
        return subject;
    }

    /**
     * @param subject the subject to set
     */
    public void setSubject(String subject) {
        this.subject = subject;
    }

    /**
     * @return the message
     */
    public String getMessage() {
        return message;
    }

    /**
     * @param message the message to set
     */
    public void setMessage(String message) {
        this.message = message;
    }

}
